/*
 * Returns an new XMLHttpRequest object, or false if the browser
 * doesn't support it.
 */
 
var prjName = "";
 
function newXMLHttpRequest() {

	var xmlreq = false;
	
	// Create XMLHttpRequest object in non-Microsoft browsers.
	if (window.XMLHttpRequest) {
		xmlreq = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) {
		try {
			// Try to create XMLHttpRequest in later versions
			// of Internet Explorer.
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			// Failed to create required ActiveXObject.
			try {
				// Try version supported by older versions
				// of Internet Explorer.
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				// Unable to create an XMLHttpRequest by any means.
				xmlreq = false;
			}
		}
	}
	return xmlreq;
}

function generateList(xmlreply, id) {
	
	var list = xmlreply.getElementsByTagName("triple")[0];
	var subjList = list.getElementsByTagName("subject");
	var predList = list.getElementsByTagName("predicate");
	var objList = list.getElementsByTagName("object");
	var subjContents = "";
	var predContents = "";
	var objContents = "";
	
	if (id === "SubjOwl") {
		predContents = predContents + "<select id='PredOwl' name='PredOwl' size=10 onClick='handleOnClick(this, \"PredOwl\");setProject(\"" + prjName + "\");setPredicate(this);setPredSelected();checkPred()'>";
		objContents = objContents + "<select id='ObjOwl' name='ObjOwl' size=10 onClick='handleRdfData(this, \"ObjOwl\", \"" + prjName + "\");setObject(this);setObjSelected();checkObj()'></select>";
		for (var i = 0; i < predList.length; i++) {
			predContents = predContents + "<option>" + predList[i].firstChild.nodeValue + "</option>";
		}
		predContents = predContents + "</select>";
		
		document.getElementById("Pred").innerHTML = predContents;
		document.getElementById("Obj").innerHTML = objContents;
	}
	else if (id === "PredOwl") {
		if (subjSelected) {
			objContents = "<select id='ObjOwl' name='ObjOwl' size=10 onClick='handleRdfData(this, \"ObjOwl\",  \"" + prjName + "\");setObject(this);setObjSelected();checkObj()'>";
			for (var j = 0; j < objList.length; j++) {
				objContents = objContents + "<option>" + objList[j].firstChild.nodeValue + "</option>";
			}
			objContents = objContents + "</select>";
			document.getElementById("Obj").innerHTML = objContents;
		} 
		else if (objSelected) {
			subjContents = "<select id='SubjOwl' name='SubjOwl' size=10 onClick='handleRdfData(this, \"SubjOwl\",  \"" + prjName + "\");setSubject(this);setSubjSelected();checkSubj()'>";
			for (var k = 0; k < subjList.length; k++) {
				subjContents = subjContents + "<option>" + subjList[k].firstChild.nodeValue + "</option>";
			}
			subjContents = subjContents + "</select>";
			document.getElementById("Subj").innerHTML = subjContents;
		}
		else {
			objContents = "<select id='ObjOwl' name='ObjOwl' size=10 onClick='handleRdfData(this, \"ObjOwl\",  \"" + prjName + "\");setObject(this);setObjSelected();checkObj()'>";
			for (var l = 0; l < objList.length; l++) {
				objContents = objContents + "<option>" + objList[l].firstChild.nodeValue + "</option>";
			}
			objContents = objContents + "</select>";
			document.getElementById("Obj").innerHTML = objContents;
			
			subjContents = "<select id='SubjOwl' name='SubjOwl' size=10 onClick='handleRdfData(this, \"SubjOwl\",  \"" + prjName + "\");setSubject(this);setSubjSelected();checkSubj()'>";
			for (var m = 0; m < subjList.length; m++) {
				subjContents = subjContents + "<option>" + subjList[m].firstChild.nodeValue + "</option>";
			}
			subjContents = subjContents + "</select>";
			document.getElementById("Subj").innerHTML = subjContents;
		}
	}
	else if (id === "ObjOwl") {
		predContents = predContents + "<select id='PredOwl' name='PredOwl' size=10 onClick='handleOnClick(this, \"PredOwl\");setProject(\"" + prjName + "\");setPredicate(this);setPredSelected();checkPred()'>";
		subjContents = subjContents + "<select id='SubjOwl' name='SubjOwl' size=10 onClick='handleRdfData(this, \"SubjOwl\",  \"" + prjName + "\");setSubject(this);setSubjSelected();checkSubj()'></select>";
		for (var n = 0; n < predList.length; n++) {
			predContents = predContents + "<option>" + predList[n].firstChild.nodeValue + "</option>";
		}
		predContents = predContents + "</select>";
		
		document.getElementById("Pred").innerHTML = predContents;
		document.getElementById("Subj").innerHTML = subjContents;
	}
}

function generateLiterals(value, xmlreply, id) {
	
	var results = xmlreply.getElementsByTagName("results")[0];
	var result = results.getElementsByTagName("result");
	var literal;
	var menuContents = "";
	
	if (id === "SubjOwl") {
		menuContents = menuContents + "<select id='SubjList' name='SubjList' style='margin: 10px;' onChange='handleLiteral(this, \"SubjList\")'><option>Choose a " + value + "...</option>";
		for (var p = 0; p < result.length; p++) {
			literal = result[p].getElementsByTagName("literal");
			if (literal[0] !== undefined) {
				if (value === "Class") {
					var ch = literal[0].firstChild.nodeValue.charAt(0);
					if (ch >= 'A' && ch <= 'Z') {
						menuContents = menuContents + "<option>" + literal[0].firstChild.nodeValue + "</option>";
					}
				}
				else {
					menuContents = menuContents + "<option>" + literal[0].firstChild.nodeValue + "</option>";
				}
			}
		}
		menuContents = menuContents + "</select>";
		document.getElementById("SubjMenu").innerHTML = menuContents;
	}
	else if (id === "ObjOwl") {
		menuContents = menuContents + "<select id='ObjList' name='ObjList' style='margin: 10px;' onChange='handleLiteral(this, \"ObjList\")'><option>Choose a " + value + "...</option>";
		for (var q = 0; q < result.length; q++) {
			literal = result[q].getElementsByTagName("literal");
			if (literal[0] !== undefined) {
				if (value === "Class") {
					var ch = literal[0].firstChild.nodeValue.charAt(0);
					if (ch >= 'A' && ch <= 'Z') {
						menuContents = menuContents + "<option>" + literal[0].firstChild.nodeValue + "</option>";
					}
				}
				else {
					menuContents = menuContents + "<option>" + literal[0].firstChild.nodeValue + "</option>";
				}
			}
		}
		menuContents = menuContents + "</select>";
		document.getElementById("ObjMenu").innerHTML = menuContents;
	}
}

function handleOnClick(item, id) {
	
	var index = item.selectedIndex;
	var val = item[index].text;
	var req = newXMLHttpRequest();
	
	req.open("POST", "ExtractorServlet", true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(id + "=" + val);
	
	req.onreadystatechange = function () {
		if (req.readyState === 4) {
			// Check that we received a successful response from the server.
			if (req.status === 200) {
				// Pass the XML payload of the response to the handler function.
				generateList(req.responseXML, id);
			} 
			else {
				// An HTTP problem has occurred.
				alert("HTTP error " + req.status + ": " + req.statusText);
			}
		}
	};
}

function handleRdfDataAjax(value, id, project) {

	var req2 = newXMLHttpRequest();
	
	req2.open("POST", "SparqlQueryServlet", true);
	req2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	if (value === "Type") {
		req2.send("PrjName=" + project + "&type=MVType");
	}
	else {
		req2.send("PrjName=" + project + "&type=" + value);
	}
	
	req2.onreadystatechange = function () {
		if (req2.readyState === 4) {
			// Check that we received a successful response from the server.
			if (req2.status === 200) {
				// Pass the XML payload of the response to the handler function.
				generateLiterals(value, req2.responseXML, id);
			} 
			else {
				// An HTTP problem has occurred.
				alert("HTTP error " + req2.status + ": " + req2.statusText);
			}
		}
	};
}

function handleRdfDataJs(value, id) {
	
	var menuSelects = "";
	
	if (id === "SubjOwl") {
		menuSelects = menuSelects + "<select id='SubjList' name='SubjList' style='margin: 10px;' onChange='handleLiteral(this, \"SubjList\")'><option>Choose a " + value + "...</option>";
		if (value === "Modifier") {
			menuSelects = menuSelects + "<option>private</option><option>protected</option><option>public</option><option>static</option>";
		}
		menuSelects = menuSelects + "</select>";
		document.getElementById("SubjMenu").innerHTML = menuSelects;
	}
	else if (id === "ObjOwl") {
		menuSelects = menuSelects + "<select id='ObjList' name='ObjList' style='margin: 10px;' onChange='handleLiteral(this, \"ObjList\")'><option>Choose a " + value + "...</option>";
		if (value === "Modifier") {
			menuSelects = menuSelects + "<option>private</option><option>protected</option><option>public</option><option>static</option>";
		}
		menuSelects = menuSelects + "</select>";
		document.getElementById("ObjMenu").innerHTML = menuSelects;
	}
}

function handleRdfData(item, id, project) {

	prjName = project;
	var index2 = item.selectedIndex;
	var val2 = item[index2].text;
	var notification = "";
	
	if (val2 === "Class" || val2 === "Method" || val2 === "MemberVariable" || val2 === "Type") {
		handleRdfDataAjax(val2, id, project);
		notification = notification + "<I><select style='margin: 10px; font: italic bold 12px arial'><option>Loading " + val2 + " list...</option></select></I>";
		if (id === "SubjOwl") {
			document.getElementById("SubjMenu").innerHTML = notification;
		}
		else if (id === "ObjOwl") {
			document.getElementById("ObjMenu").innerHTML = notification;
		}
	}
	else {
		handleRdfDataJs(val2, id);
	}
}

// This function is only called when the user selects a Predicate first.
function setProject(project) {
	prjName = project;
}